<?php
// +---------------------------------------------------------------------------------
// | ECODE [ 随心而行 勿忘始终]
// +---------------------------------------------------------------------------------
// | Copyright (c) 2017 ECODE All rights reserved.
// +---------------------------------------------------------------------------------
// | Author: 鬼谷子 <majianwei168@outlook.com>
// +---------------------------------------------------------------------------------
namespace app\admin\controller;
use app\admin\logic\Menu as MenuLogic;
class Menu extends Base
{

    protected $beforeActionList = [
        'parentTitle'  =>  ['only'=>'add,edit'],
    ];

    
    //编辑与新增的前置方法
    protected function parentTitle()
    {
    	if(!$this->isPost()) {
	    	$pid = input('pid');
	    	$data = $this->M()->getPidMenu($pid);
	    	$this->assign('parentTitle', $data);
    	}
    }


	//测试
	public function testEdit()
	{
		$data = $this->M()->getPidMenu(0);
		dump($data);
	}

	//列表
    public function index()
    {
    	//变量定义
    	$id 	= 0;

    	//参数处理
    	$param = input();
    	if(!empty($param)) {
    		$id 			= $param ['id'];
    	}
    	$map ['pid'] 	= $id;

    	//数据处理
    	$data = $this->M()->dataList($map);

    	//模板赋值
    	$this->assign('id', $id);
		$this->assign('list', $data['list']);
		$this->assign('page', $data['page']);
 		return view();
    }
    //添加
    public function add($pid=0)
    {
    	if($this->isPost()) {
    		$post = input();
    		$result = $this->M()->dataAdd($post);
    		if($result ['bool']) {
    			$this->success($this->getTipMessage('success'), url('index'));
    		} else {
    			$this->error($this->getTipMessage($result['code']));
    		}

    	} else {
    		return view('edit');
    	}
    	
    }
    //编辑
    public function edit($id='')
    {
    	if($this->isPost()) {
    		$post = input();
    		$result = $this->M()->dataUpdate($post, $post ['id']);
    		if($result ['bool']) {
    			$this->success($this->getTipMessage('success'), url('index'));
    		} else {
    			$this->error($this->getTipMessage($result['code']));
    		}
    	} else {
    		$result = $this->M()->dataFind($id);
    		$this->assign('test', 'test');
    		$this->assign('data', $result ['data']);
    		return view('edit');
    	}
    }
    //删除
    public function del($id)
    {}
    //批量删除
    public function dels($ids)
    {}

    //实例逻辑类
    private function M()
    {
    	return new MenuLogic();
    }

}